package com.business;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baidu.location.demo.R;
import com.entity.ProgramVersion;
import com.entity.Response;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogRootLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogView;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.util.DialogUtil;
import com.util.HttpUtils;
import com.util.util;

public class UpdateApp {
    /**
     * 是否存在新版本App
     * */
    public static void IsExitNewApp(Activity activity){
        new Thread(() -> UpdateAppTitle(activity)).start();
    }
    /**
     * 存在新版本弹出提示框
     * */
    private static void UpdateAppTitle(Activity activity) {
        final TextView[] downloadSchedule = new TextView[1];
        Response<ProgramVersion> data= JSONObject.parseObject(HttpUtils.Get("ProgramVersion/GetProgramVersion?Identification=app"),new TypeReference<Response<ProgramVersion>>(){});
        if(data!=null&&data.getStatusCode()==200){
            PackageManager manager = activity.getPackageManager();
            PackageInfo info = null;
            try {
                info = manager.getPackageInfo(activity.getPackageName(), 0);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            if(!data.getValue().getVersions().equals(info.versionName)){
                activity.runOnUiThread(() -> {
                    ClipboardManager cmb = (ClipboardManager)activity.getSystemService(Context.CLIPBOARD_SERVICE);
                    cmb.setText(data.getValue().getDownloadLink());
                    new QMUIDialog.MessageDialogBuilder(activity)
                            .setTitle("新版本通知")
                            .setMessage("app存在新的版本是否下载最新版本app？")
                            .addAction("取消", (dialog, index) -> dialog.dismiss())
                            .addAction("确定", (dialog, index) -> {
                                dialog.dismiss();
                                QMUIDialog.CustomDialogBuilder customDialog=new QMUIDialog.CustomDialogBuilder(activity){
                                    @Nullable
                                    @Override
                                    protected View onCreateContent(QMUIDialog dialog, QMUIDialogView parent, Context context) {

                                        return super.onCreateContent(dialog, parent, context);
                                    }

                                    @Override
                                    protected void onAfterCreate(@NonNull QMUIDialog dialog, @NonNull QMUIDialogRootLayout rootLayout, @NonNull Context context) {
                                        super.onAfterCreate(dialog, rootLayout, context);
                                        downloadSchedule[0] =rootLayout.findViewById(R.id.downloadSchedule);
                                    }
                                }.setLayout(R.layout.downloadlayout);
                                customDialog.addAction("取消显示", (dialog1, index1) -> {
                                    dialog1.dismiss();
                                });
                                customDialog.create();
                                QMUIDialog dialog1= customDialog.show();
                                new Thread(){
                                    @Override
                                    public void run() {
                                        super.run();
                                        HttpUtils.downloadFile(activity, data.getValue().getDownloadLink(), new HttpUtils.IDownload() {
                                            @SuppressLint("SetTextI18n")
                                            @Override
                                            public void DownloadProgress(double progress) {
                                                Log.d("TAG", "进度："+progress);
                                                activity.runOnUiThread(() -> downloadSchedule[0].setText(progress+"%"));
                                            }

                                            @Override
                                            public void DownloadSucceed(boolean isSucceed, long elapsedTime, String path) {
                                                if(isSucceed){
                                                    activity.runOnUiThread(dialog1::dismiss);
                                                    util.installApk(activity,path);
                                                }
                                            }

                                            @Override
                                            public void DownloadException(Exception e) {

                                            }
                                        });
                                    }
                                }.start();
                            })
                            .show();
                });
            }
        }else{
            DialogUtil.CreateDoalog(activity,data.getMessage(),0, QMUITipDialog.Builder.ICON_TYPE_NOTHING);
        }
    }

}
